package com.snts.synchronization.collector;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentLinkedQueue;

import com.snts.synchronization.transaction.Transaction;

public class UpdateQueueImpl implements UpdateQueue {

	private Map<UUID, ConcurrentLinkedQueue<Transaction>> updateQueues = Collections.synchronizedMap(
			                                                   new HashMap<UUID, ConcurrentLinkedQueue<Transaction>>());
	
	/* (non-Javadoc)
	 * @see com.snts.synchronization.core.UpdateQueue#add(java.util.UUID, com.snts.synchronization.core.Transaction)
	 */
	@Override
	public void add(UUID restaurantId, Transaction transaction){
		ConcurrentLinkedQueue<Transaction> queue = updateQueues.get(restaurantId);
		if(queue == null){
			queue = new ConcurrentLinkedQueue<Transaction>();
			updateQueues.put(restaurantId, queue);
		}
		queue.offer(transaction);
	}

	/* (non-Javadoc)
	 * @see com.snts.synchronization.core.UpdateQueue#poll(java.util.UUID)
	 */
	@Override
	public Transaction poll(UUID restaurantId){
		ConcurrentLinkedQueue<Transaction> queue = updateQueues.get(restaurantId);
		if(queue != null){
			return queue.poll();
		}
		return null;
	}

}
